The following sections provide examples of using
PowerShell both for general tasks and for SQL Server 2008–specific
tasks. We expand on some of the basic concepts introduced earlier with
SQL Server 2008–specific examples.
General Tasks
Often you might be required to send out emails containing particular reports and/or output from commands run.
To do so, you use features from the .NET Framework via PowerShell to send out emails, as shown in here:
Function Send-Mail {
param([string]$To,[string]$From,[string]$Subject, `
[string]$Body,[string]$File,[string]$SmtpServer)
If($SmtpServer -eq ""){
$SmtpServer = "FQDN of your SMTP server here"
}
$Smtp = New-Object System.Net.Mail.SMTPclient($SmtpServer)
$Message = New-Object
System.Net.Mail.MailMessage($From,$To,$Subject,$Body)
If ($File -ne "") {
$Attach = New-Object System.Net.Mail.Attachment $File
$Message.Attachments.Add($Attach)
}
$smtp.Send($message)
}
You
can enter the preceding code into a script or directly to the console.
If you type the code in the console, you must press the Enter key twice
(once to close the function and another time on an empty line) before
the PowerShell prompt returns.
In the preceding
code listing, functionality from the .NET Framework is used to get SMTP
functionality. A function is used so that this code could be easily
copied as required into new scripts, and so on. Calling the function is
then easy, and passing the command-line arguments is shown here (the
PowerShell prompt can vary depending on whether the default PowerShell
is used or the new SQL minishell):
PS>Send-Mail -To "[email protected] " -From "[email protected]" –Subject
"Automated Email" -Body "Testing" -File "C:\reports\report.txt"
Note
You might need to configure some antivirus programs to allow the PowerShell.exe process (or sqlps.exe) to “talk” over the SMTP protocol port (TCP 25).
Scheduling Scripts
From time to time, it may be
useful to have a method to schedule PowerShell scripts to run
automatically based on a particular schedule (when the SQL Server Agent
isn’t available locally, for example).
You can easily view the method to call PowerShell scripts by simply typing powershell.exe /? from a PowerShell session, as shown here:
PS>powershell.exe /?
...
PowerShell -Command "& {Get-EventLog -LogName security}"
...
Only a very small section of the text displayed is shown in this example. The powershell.exe can be used for scheduling regular PowerShell scripts. sqlps.exe works similarly, and you can also access its help by passing a slash and question mark (/?) to the command:
PS SQLSERVER:\> sqlps.exe /?
sqlps [ [ [-NoLogo] [-NoExit] [-NoProfile]
[-OutputFormat {Text | XML}] [-InputFormat {Text | XML}]
]
[-Command { -
| <string> [ <command_parameters> ]
| <script_block> [ -args <argument_array> ]
}
]
]
[ -Help | -?]
-NoLogo
Do not display the copyright banner on startup.
-NoExit
Keep running after completing all startup commands.
-NoProfile
Do not load a user profile.
-OutputFormat
Format the output of all objects as either text strings (Text) or in a
serialized CLIXML format (XML).
-InputFormat
The input from stdin is formatted as either text strings (Text) or in a
serialized CLIXML format (XML).
-Command
sqlps runs the commands specified and then exits, unless -NoExit is also
specified. Do not specify other characters after the -Command switch,
they will be read as command arguments.
-
Read input commands from the keyboard by using stdin.
<string> [ <command_parameters> ]
Specifies a string containing the PowerShell commands to be run. Use
the format "&{<command>}". The quotation marks identify a string and
the invocation operator (&) causes sqlps to run the command.
<script_block> [ -args <argument_array> ]
Specifies a block of PowerShell commands to be run. Use the format
{<script_block>}.
-Help | -?
Show the syntax summary help.
Note
How do you know whether to use powershell.exe or sqlps.exe when scheduling jobs? If you’re using anything relating to SMO and/or the SQL cmdlets in the script, sqlps.exe
would seem to be easier to use because all the prerequisites to using
SMO and the SQL cmdlets are already loaded, which can save several lines
in a script. As a reminder, the SQL minishell is limited in its
functionality, so powershell.exe may be required in particular if you need to load some PowerShell functionality from another application, such as Exchange.
As discussed briefly earlier, SQL Server Agent can also be used to run scheduled PowerShell commands.